﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommunergyLogic;
using System.Data.Linq;

namespace CommunergyWinFormUtilities
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var db = new communergyEntities();
            var meters = db.ElectricityMeter.Include("ElectricityDaily_Manual").Include("ElectricityDaily_ManualReading");
            foreach (var meter in meters)
            {
                foreach (var oldReading in meter.ElectricityDaily_ManualReading.ToList())
                {
                    meter.ElectricityDaily_ManualReading.Remove(oldReading);
                    db.DeleteObject(oldReading);
                }
            }
            db.SaveChanges();

            foreach (var meter in meters)
            {


                ElectricityDaily_Manual firstDailyReading = meter.ElectricityDaily_Manual.FirstOrDefault();
                ElectricityDaily_ManualReading firstReading = new ElectricityDaily_ManualReading { mr_dateTime = firstDailyReading.de_dateTime, mr_reading = firstDailyReading.de_kWhTotal.Value };
                meter.ElectricityDaily_ManualReading.Add(firstReading);
                // db.AddToElectricityDaily_ManualReading(firstReading);
                //db.SaveChanges();

                ElectricityDaily_Manual lastDailyReading = firstDailyReading;


                foreach (var dailyReading in meter.ElectricityDaily_Manual)
                {
                    if (dailyReading.de_kWhDaily != lastDailyReading.de_kWhDaily)
                    {
                        ElectricityDaily_ManualReading newReading = new ElectricityDaily_ManualReading
                                                                    {
                                                                        ElectricityMeter = meter,
                                                                        mr_dateTime = lastDailyReading.de_dateTime,
                                                                        mr_reading = lastDailyReading.de_kWhTotal.Value
                                                                    };
                        db.AddToElectricityDaily_ManualReading(newReading);

                    }
                    lastDailyReading = dailyReading;
                }
                ElectricityDaily_ManualReading lastReading = new ElectricityDaily_ManualReading
                {
                    ElectricityMeter = meter,
                    mr_dateTime = lastDailyReading.de_dateTime,
                    mr_reading = lastDailyReading.de_kWhTotal.Value
                };
                db.AddToElectricityDaily_ManualReading(lastReading);
            }
            db.SaveChanges();

        }
    }
}
